package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.model.Build;
import com.model.PageBean;
import com.util.StringUtil;
/**
 * @author wangjg
 * @date 2023/11/6 15:15
 * @Description: 楼宇管理 DAO
 */
public class BuildDao {

    public List<Build> buildList(Connection con, PageBean pageBean, Build build)throws Exception {
        List<Build> buildList = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from t_build t1");
        if(StringUtil.isNotEmpty(build.getName())) {
            sb.append(" where t1.name like '%").append(build.getName()).append("%'");
        }
        sb.append(" order by t1.num ");
        if(pageBean != null) {
            sb.append(" limit ").append(pageBean.getStart()).append(",").append(pageBean.getPageSize());
        }
        PreparedStatement preparedStatement = con.prepareStatement(sb.toString());
        ResultSet rs = preparedStatement.executeQuery();
        while(rs.next()) {
            Build build1 = new Build();
            build1.setId(rs.getInt("id"));
            build1.setName(rs.getString("name"));
            build1.setNum(rs.getString("num"));
            build1.setDetail(rs.getString("detail"));
            buildList.add(build1);
        }
        return buildList;
    }

    public Build buildInfo(Connection con, int id)throws Exception {
        String sql = "select * from t_build where id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        ResultSet rs = preparedStatement.executeQuery();
        if(rs.next()) {
            Build build = new Build();
            build.setId(rs.getInt("id"));
            build.setName(rs.getString("name"));
            build.setNum(rs.getString("num"));
            build.setDetail(rs.getString("detail"));
            return build;
        }
        return null;
    }

    public int buildCount(Connection con, Build build)throws Exception {
        StringBuilder sb = new StringBuilder("select count(*) as total from t_build t1");
        if(StringUtil.isNotEmpty(build.getName())) {
            sb.append(" where t1.name like '%").append(build.getName()).append("%'");
        }
        PreparedStatement preparedStatement = con.prepareStatement(sb.toString());
        ResultSet rs = preparedStatement.executeQuery();
        if(rs.next()) {
            return rs.getInt("total");
        } else {
            return 0;
        }
    }


    public int addBuild(Connection con, Build build)throws Exception {
        String sql = "insert into t_build values(null,?,?,?)";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, build.getName());
        preparedStatement.setString(2, build.getNum());
        preparedStatement.setString(3, build.getDetail());
        return preparedStatement.executeUpdate();
    }

    public int deleteBuild(Connection con, String id)throws Exception {
        String sql = "delete from t_build where id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, id);
        return preparedStatement .executeUpdate();
    }

    public int updateBuild(Connection con, Build build)throws Exception {
        String sql = "update t_build set name=?,detail=? ,num=? where id=?";
        PreparedStatement preparedStatement = con.prepareStatement(sql);
        preparedStatement.setString(1, build.getName());
        preparedStatement.setString(2, build.getDetail());
        preparedStatement.setString(3, build.getNum());
        preparedStatement.setInt(4, build.getId());
        return preparedStatement.executeUpdate();
    }

}
